package com.example.demo.nuoqi.controller;

import com.example.demo.nuoqi.pojo.Admin;
import com.example.demo.nuoqi.pojo.Profession;
import com.example.demo.nuoqi.service.ProfessionService;
import com.example.demo.nuoqi.utils.Result;
import com.example.demo.nuoqi.utils.StatusCode;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "ProfessionController")
@RestController
@RequestMapping("/profession")
@CrossOrigin
public class ProfessionController {

    @Autowired
    private ProfessionService professionService;

    /***
     * Profession分页条件搜索实现
     * @param profession
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Profession条件分页查询",notes = "分页条件查询Profession方法详情",tags = {"ProfessionController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Profession对象",value = "传入JSON数据",required = false) Profession profession, @PathVariable int page, @PathVariable int size){
        //调用ProfessionService实现分页条件查询Profession
        PageInfo<Profession> pageInfo = professionService.findPage(profession, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Profession分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Profession分页查询",notes = "分页查询Profession方法详情",tags = {"ProfessionController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size){
        //调用ProfessionService实现分页查询Profession
        PageInfo<Profession> pageInfo = professionService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     *
     * @param profession
     * @return
     */
    @ApiOperation(value = "Profession条件查询",notes = "条件查询Profession方法详情",tags = {"ProfessionController"})
    @PostMapping(value = "/search" )
    public Result<List<Profession>> findList(@RequestBody(required = false) @ApiParam(name = "Profession对象",value = "传入JSON数据",required = false) Profession profession){
        //调用ProfessionService实现条件查询Profession
        List<Profession> list = professionService.findList(profession);
        return new Result<List<Profession>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Profession根据ID删除",notes = "根据ID删除Profession方法详情",tags = {"ProfessionController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用ProfessionService实现根据主键删除
        professionService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Profession数据
     * @param profession
     * @param id
     * @return
     */
    @ApiOperation(value = "Profession根据ID修改",notes = "根据ID修改Profession方法详情",tags = {"ProfessionController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Profession对象",value = "传入JSON数据",required = false) Profession profession, @PathVariable Integer id){
        //设置主键值
        profession.setId(id);
        //调用ProfessionService实现修改Profession
        professionService.update(profession);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Profession数据
     * @param profession
     * @return
     */
    @ApiOperation(value = "Profession添加",notes = "添加Profession方法详情",tags = {"ProfessionController"})
    @PostMapping
    public Result add(@RequestBody @ApiParam(name = "Profession对象",value = "传入JSON数据",required = true) Profession profession){
        //调用ProfessionService实现添加Profession
        professionService.add(profession);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Profession数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Profession根据ID查询",notes = "根据ID查询Profession方法详情",tags = {"ProfessionController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<Profession> findById(@PathVariable Integer id){
        //调用ProfessionService实现根据主键查询Profession
        Profession profession = professionService.findById(id);
        return new Result<Profession>(true,StatusCode.OK,"查询成功",profession);
    }

    /***
     * 查询Profession全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Profession",notes = "查询所Profession有方法详情",tags = {"ProfessionController"})
    @GetMapping
    public Result<List<Profession>> findAll(){
        //调用ProfessionService实现查询所有Profession
        List<Profession> list = professionService.findAll();
        return new Result<List<Profession>>(true, StatusCode.OK,"查询成功",list) ;
    }


    @GetMapping("/setProfessStatus/{professId}/{result}")
    @ApiOperation(value = "设置用户状态 0启用 1禁用", notes = "设置状态 0启用 1禁用", tags = {"ProfessionController"})
    public Result setProfessStatus(@PathVariable("professId")Integer userId,@PathVariable("result") String result) {
        Profession byId = professionService.findById(userId);
        if ("0" == result) {
            byId.setStatus("0");
        }else {
            byId.setStatus("1");
        }
        return new Result(true, StatusCode.OK, "设置专业状态成功", null);
    }
}
